﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

namespace zevenseas.Community.Web.UI.WebControls
{
    public class zsSearchBox : System.Web.UI.WebControls.WebParts.WebPart
    {
        private TextBox searchBox = new TextBox();
        private ImageButton searchImage = new ImageButton();
        private int searchboxwidth = 420;
        private bool showadvancedsearchlink = true;
        private string advancedsearchpageurl = "/SearchCenter/Pages/advanced.aspx";
        private string searchresultpageurl = "/search.aspx";
        private SPWeb currentWeb = SPContext.Current.Web;
        
        [WebBrowsable(true), Personalizable(true), SPWebCategoryName("Settings")]
        public int SearchBoxWidth
        {
            get { return searchboxwidth; }
            set { searchboxwidth = value; }
        }

        [WebBrowsable(true), Personalizable(true), SPWebCategoryName("Settings")]
        public bool ShowAdvancedSearchLink
        {
            get { return showadvancedsearchlink; }
            set { showadvancedsearchlink = value; }
        }

        [WebBrowsable(true), Personalizable(true), SPWebCategoryName("Settings")]
        public string AdvancedSearchPageURL
        {
            get { return advancedsearchpageurl; }
            set { advancedsearchpageurl = value; }
        }

        [WebBrowsable(true), Personalizable(true), SPWebCategoryName("Settings")]
        public string SearchResultPageURL
        {
            get { return searchresultpageurl; }
            set
            {
                char[] trimmer = {'/'};
                searchresultpageurl = value.TrimStart(trimmer);
            }
        }

        protected override void CreateChildControls()
        {
            searchImage.ImageUrl = "/_layouts/images/gosearch.gif";
            searchImage.Click += SubmitSearch;
            searchImage.ID = "SearchImage";

            searchBox.Width = new Unit(searchboxwidth);
            searchBox.BorderStyle = System.Web.UI.WebControls.BorderStyle.None;
            searchBox.Text = HttpContext.Current.Request.QueryString["k"];
            searchBox.Attributes.Add("onkeydown", "javascript:if((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)){document.getElementById('" + base.ClientID + "_" + searchImage.ClientID + "').click(); return false;} else return true;");
          
            Controls.Add(searchBox);
            Controls.Add(searchImage);
        }

        private void SubmitSearch(object sender, EventArgs e)
        {
            //UrlUtilities urlUtils = new UrlUtilities();
            //string baseURL = urlUtils.GetUrlWithTrailingSlash(currentWeb.Url) + searchresultpageurl;

            string baseURL = currentWeb.Url + @"/" + searchresultpageurl;
            string searchUrl = baseURL + "?k=" + searchBox.Text + "&u=" + currentWeb.Url;
            HttpContext.Current.Response.Redirect(searchUrl);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.Write("<table class='ms-sbtable ms-sbtable-ex' border='0'>");
            writer.Write("<tr class='ms-sbrow'>");
            writer.Write("<td class='ms-sbcell'>");
            searchBox.RenderControl(writer);
            writer.Write("</td><td class='ms-sbgo ms-sbcell'>");
            searchImage.RenderControl(writer);
            writer.Write("</td><td class='ms-sbLastcell'></td>");

            if (showadvancedsearchlink)
            {
                writer.Write("<td class='ms-sbcell ms-sblink'><a title='Advanced Search' href='" + AdvancedSearchPageURL +
                             "'>Advanced Search</a></td>");
            }
            writer.Write("</tr></table>");
        }
    }
}